package cn.afactory.tygz.ui.base.views.search;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.controller.SearchController;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.entity.LoganHotWordEntity;
import cn.afactory.tygz.ui.base.entity.SearchHistoryEntity;

/**
 * Created by Kenneth on 15/9/10.
 */
public class SearchMainFragment extends BaseFragment {

    private SearchSourcePager mSearchSourcePager = SearchSourcePager.COMPANY;
    private SearchType mSearchType = SearchType.COMPANY;
    private ViewGroup mHotKeyWordAndHistoryPanel;
    private HotSearchView mHotSearchKeywordView;
    private SearchHistoryView mSearchHistoryView;
    private SearchController mController;

    public static SearchMainFragment newInstance() {
        SearchMainFragment smf = new SearchMainFragment();
        return smf;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
//            case SearchController.MSG_KEY_HOT_WORD:
//                if (msg.obj instanceof List<?>) {
//                    onHotKeyWordLoaded((List<LoganHotWordEntity>) msg.obj);
//                }
//                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addTitleBar = false;
        mController = new SearchController(mContext, mUIHandler);
    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_search_main_layout, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onTitleCreate();
        initViews(view);
        startLoadData();
    }

    public void onTitleCreate() {

    }

    private void initViews(View view) {

        mHotKeyWordAndHistoryPanel = (ViewGroup) view.findViewById(R.id.search_hot_keyword_history_panel);

        mHotSearchKeywordView = (HotSearchView) view.findViewById(R.id.search_hot_keyword_view);
        mSearchHistoryView = (SearchHistoryView) view.findViewById(R.id.search_history_view);

        mHotSearchKeywordView.setOnItemViewClickListener(new HotSearchView.OnItemViewClickListener() {
            @Override
            public void onItemClick(LoganHotWordEntity loganHotWordEntity) {
                performSearch(loganHotWordEntity.content);
            }
        });

        mSearchHistoryView.setOnItemClickListener(new SearchHistoryView.OnViewClickListener() {
            @Override
            public void OnItemClick(View v, int position, SearchHistoryEntity item) {
                performSearch(item.content);
            }

            @Override
            public void onCacheClear() {
                SearchHistory.getInstance(mContext).clearHistory();
            }
        });

    }

    /**
     * @param content
     */
    private void performSearch(String content) {
//        if (mActivity instanceof SearchActivity) {
//            ((SearchActivity) mActivity).startSearch(mSearchType, content);
//        }
    }

    /**
     * @param type
     */
    public void setSearchPagerAndType(SearchSourcePager searchSourcePager, SearchType type) {
        mSearchSourcePager = searchSourcePager;
        mSearchType = type;
    }

    /**
     * 开始加载数据
     */
    public void startLoadData() {

    }

    /**
     * 热词列表加载完成
     *
     * @param hwList
     */
    private void onHotKeyWordLoaded(List<LoganHotWordEntity> hwList) {
        if (!hwList.isEmpty()) {
            mHotSearchKeywordView.setVisibility(View.VISIBLE);
            mHotSearchKeywordView.setData(hwList);
        } else {
            mHotSearchKeywordView.setVisibility(View.GONE);
        }
    }
}
